package com.dup.jxlh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dup.jxlh.admin.entities.model.Role;
import com.dup.jxlh.admin.entities.vo.PermVO;
import com.dup.jxlh.admin.entities.vo.RoleVO;
import com.dup.jxlh.admin.mapper.RoleMapper;
import com.dup.jxlh.admin.service.OwnValidatorService;
import com.dup.jxlh.admin.service.RoleService;
import com.dup.jxlh.admin.service.auth.AuthService;
import com.dup.jxlh.api.entities.PaginationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;

@Service("roleServiceImpl")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Autowired
    private AuthService authService;

    @Autowired
    private OwnValidatorService ownValidatorService;

    @Override
    public Page<Role> getRoleList(PaginationData paginationData) {
        Page<Role> rolePage = new Page<>(paginationData.getCurrent(), paginationData.getSize());
        rolePage.setOptimizeCountSql(false);

        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        ownValidatorService.setQueryWrapperByCompany(queryWrapper, paginationData);

        if (!paginationData.getSearch().isEmpty()) {
            queryWrapper.like("name", paginationData.getSearch());
        }

        ArrayList<Role> roleList = roleMapper.getRoleList(rolePage, queryWrapper);
        if (roleList != null) {
            rolePage.setRecords(roleList);
        }
        return rolePage;
    }

    @Override
    @Cacheable(value = "role", key = "#roleId")
    public Role getRole(BigInteger roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public Boolean addRole(Role role) {
        if(roleMapper.insert(role) == 1){
            return true;
        }
        return false;
    }

    @Override
    @CacheEvict(value = "role", key = "#role.id")
    public Boolean editRole(Role role) {
        if(roleMapper.updateById(role) == 1){
            return true;
        }
        return false;
    }

    @Override
    @Cacheable(value = "mainperm")
    public ArrayList<PermVO> getMainPerm() {
        return roleMapper.getMainPerm();
    }

    @Override
    @Cacheable(value = "roleperm", key = "#roleId")
    public BigInteger[] getRolePermIds(BigInteger roleId) {
        return roleMapper.getRolePermIds(roleId);
    }

    @Override
    @Transactional
    @CacheEvict(value = "roleperm", key = "#roleVO.id")
    public Boolean updateRolePerm(RoleVO roleVO) {
        roleMapper.deleteRolePerm(roleVO.getId());
        roleMapper.insertRolePerm(roleVO);
        String[] usernames = roleMapper.getRoleUsernames(roleVO.getId());
        for (int i = 0; i < usernames.length; i++) {
            authService.createCurrentAuthPermFlag(usernames[i]);
        }
        return true;
    }
}
